using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using BTV.Base;

namespace BTV.TVAnyTime
{
    public class TVAnyServiceInformationParser
    {
        private List<BTVChannel> _channels = new List<BTVChannel>();

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1002:DoNotExposeGenericLists"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public List<BTVChannel> Channels
        {
            get { return _channels; }
            set { _channels = value; }
        }
        private string _url;

        public TVAnyServiceInformationParser(string url)
        {
            this._url = url;
        }

        public TVAnyServiceInformationParser()
        {

        }

        public BTVChannel GetChannelByName(string name)
        {
            foreach ( BTVChannel c in Channels )
            {
                if ( c.Name == name )
                    return c;
            }
            return null;
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1804:RemoveUnusedLocals", MessageId = "ioe")]
        public void ParseFile()
        {
            XmlTextReader r = new XmlTextReader(_url);
            BTVChannel c = new BTVChannel();
            String currentTag = "";
            bool cName = false;
            try
            {
                while (r.Read())
                {
                    r.MoveToContent();
                    switch (r.NodeType)
                    {
                        case XmlNodeType.Element:
                            currentTag = r.Name.ToLower();
                            switch (currentTag)
                            {
                                case "serviceinformation":
                                    //Console.WriteLine("new TVAny_Program");
                                    c = new BTVChannel();
                                    c.Id = r.GetAttribute("serviceId");
                                    break;
                            }
                            break;
                        case XmlNodeType.Text:
                        case XmlNodeType.CDATA:
                            switch (currentTag)
                            {
                                case "name":
                                    if (!cName)
                                    {
                                        cName = true;
                                        c.Name = r.Value;
                                    }
                                    break;

                            }
                            break;
                        case XmlNodeType.EndElement:
                            switch (r.Name.ToLower())
                            {
                                case "serviceinformation":
                                    //Console.WriteLine("end TVAny_Program");
                                    //tv = new TVAny_Program(r.GetAttribute("programId"));
                                    Channels.Add(c);
                                    cName = false;
                                    break;
                            }
                            break;
                    }
                }
            }
            catch 
            {
                /*ignore*/
            }
        }
    }
}
